<?php

namespace App\Console\Scripts\Counter;

use App\Consts\GlobalConst;
use App\Consts\GlobalCounterConst;
use App\Models\Cache\CacheKey;
use App\Models\IndexComment;
use App\Models\SysReply;
use App\Models\UserMessageLikeSend;
use Illuminate\Database\Query\Builder;

/**
 * 评论计数统计脚本
 * Class ArticleCounterCommand
 * @package App\Console\Scripts\Counter
 */
class CommentCounterCommand extends BaseCounterCommand
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'counter:comment {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'counter for comment';

    /**
     * 评论点赞统计
     * @return int
     */
    public function like()
    {
        $subMode = $this->_getSubModel();
        $finish = $subMode->count();
        // 每 100 单位合并处理一次
        $subMode->chunkById(100, function ($list) use ($finish) {
            foreach ($list as $item) {
                static $index = 1;
                $id = $item->comment_id;
                $total = UserMessageLikeSend::getSubModel(function (Builder $query) use ($id) {
                    $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
                    $query->where('target_type', GlobalConst::TARGET_TYPE_COMMENT);
                    $query->where('target_id', $id);
                }, ['id'])->count();
                // set cache
                $cacheKey = $this->setHashCache(
                    ['comment_id' => $id],
                    CacheKey::COUNTER_COMMENT,
                    [GlobalCounterConst::COUNT_LIKE => $total]
                );
                // log info
                $info = "[$index/$finish] set cache key `$cacheKey` - ".GlobalCounterConst::COUNT_LIKE." {$this->sort} to (int) $total";
                $this->info($info);
                if ($total) $this->logger($info, false, 'script');
                $index++;
            }
        }, 'comment_id');
        return 1;
    }

    /**
     * 评论回复统计
     * @return int
     */
    public function reply()
    {
        $subMode = $this->_getSubModel();
        $finish = $subMode->count();
        // 每 100 单位合并处理一次
        $subMode->chunkById(100, function ($list) use ($finish) {
            foreach ($list as $item) {
                static $index = 1;
                $id = $item->comment_id;
                $total = SysReply::getSubModel(function (Builder $query) use ($id) {
                    $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
                    $query->where('target_type', GlobalConst::TARGET_TYPE_COMMENT);
                    $query->where('target_id', $id);
                }, ['id'])->count();
                // set cache
                $cacheKey = $this->setHashCache(
                    ['comment_id' => $id],
                    CacheKey::COUNTER_COMMENT,
                    [GlobalCounterConst::COUNT_REPLY => $total]
                );
                // log info
                $info = "[$index/$finish] set cache key `$cacheKey` - ".GlobalCounterConst::COUNT_REPLY." {$this->sort} to (int) $total";
                $this->info($info);
                if ($total) $this->logger($info, false, 'script');
                $index++;
            }
        }, 'comment_id');
        return 1;
    }

    /**
     * get sub comment
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     */
    private function _getSubModel()
    {
        //$subMode = SysComment::getSubModel(function (Builder $query) {
        //    $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
        //}, ['id', 'comment_id']);
        $subMode = IndexComment::getModel()
            ->select(['id', 'comment_id', 'reply_id'])
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
            ->where('is_shield', GlobalConst::IS_NOT_SHIELD);
        return $subMode;
    }

}
